package cn.shiro.shiroservice.auth.controller;

import cn.shiro.shiroservice.auth.pojo.vo.in.LoginInVO;
import cn.shiro.shiroservice.auth.pojo.vo.out.UserInfoOutVo;
import cn.shiro.shiroservice.auth.service.AuthService;
import cn.shiro.shiroservice.auth.utils.ShiroUtils;
import cn.shiro.shiroservice.common.utils.R;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * &#064;Time 2024 一月 星期四 13:16
 *
 * @author ShangGuan
 */
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {

    private final AuthService authService;

    @PostMapping("/login")
    public R<UserInfoOutVo> login(@RequestBody LoginInVO loginParam) {
        UserInfoOutVo login = authService.login(loginParam);
        Subject subject = ShiroUtils.getSubject();
        return R.ok(login);
    }

    @GetMapping("/save")
//    @RequiresAuthentication
//    @RequiresPermissions(value = {"ORDINARY"}, logical = Logical.AND)
//    @RequiresRoles(value = {"ADMIN"})
    public R<String> save() {
        Subject subject = ShiroUtils.getSubject();

        System.out.println("---------isPermitted ORDINARY--------" + SecurityUtils.getSubject().isPermitted("ORDINARY"));
        System.out.println("---------hasRole ADMIN--------" + SecurityUtils.getSubject().hasRole("ADMIN"));

//        Subject subject = SecurityUtils.getSubject();
//        boolean admin = subject.isPermitted("ORDINARY");
//        boolean superAdmin = subject.hasRole("ADMIN");
//
//        System.out.println(subject.getPrincipal());

        return R.ok("save");
    }

}
